package com.winderinfo.yikaotianxia.ui.address;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.register.RegisterBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class AddressActivity extends BaseActivity implements Contract.View {

    @BindView(R.id.addaddress_recycle)
    RecyclerView addaddressRecycle;
    private Presenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private String userID;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    protected void initView() {
        presenter = new Presenter(this);
        SPUtils spUtils = new SPUtils(this);
        //获取用户Id
        userID = spUtils.getString("UserID", "");
        addaddressRecycle.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        //我的地址
        /**
         * @userId 用户id
         * */
        map.clear();
        map.put("userId", "1");
        presenter.onPostRequest(Api.ADDRESSLIST, map, AddresListBean.class);
    }


    @Override
    public void onSuccess(Object data) {
        //地址列表
        if (data instanceof AddresListBean) {
            List<AddresListBean.RowsBean> rows = ((AddresListBean) data).getRows();
            AddressAdapter addressAdapter = new AddressAdapter(this, rows);
            addaddressRecycle.setAdapter(addressAdapter);
            //删除地址
            addressAdapter.setOnaddDel(new AddressAdapter.OnaddDel() {
                @Override
                public void delId(int id) {
                    /**
                     * @ids 地址id
                     * */
                    map.clear();
                    map.put("ids", id);
                    presenter.onPostRequest(Api.DELADDRESS, map, RegisterBean.class);
                }
            });
        }
        //删除地址
        if (data instanceof RegisterBean) {
            if (((RegisterBean) data).getCode() == 0) {
                ToastUtil.showSuccess(this, "删除成功");
                map.clear();
                map.put("userId", "1");
                presenter.onPostRequest(Api.ADDRESSLIST, map, AddresListBean.class);
            } else if (((RegisterBean) data).getCode() == 500) {
                ToastUtil.showSuccess(this, "删除失败");
            }
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

    @OnClick({R.id.addaddress_back, R.id.addaddress_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.addaddress_back:
                finish();
                break;
            case R.id.addaddress_add:
                openActivity(AddAddressActivity.class);
                break;
        }
    }
}
